package master;

/**
 * @author chenyw
 * @date 2022/8/20 9:57
 *
 * 位1的个数
 *
 */
public class M191hammingWeight {

    //n & (n - 1)：把n的最低位的1变成零
    public int hammingWeight(int n) {
        int res = 0;
        while (n != 0) {
            n &= n - 1;
            res++;
        }
        return res;
    }


/*    //遍历
    public int hammingWeight(int n) {
        int count = 0;
        for (int i = 0; i < 32; i++) {
            if ((n & 1) == 1) {
                count++;
            }
            n = n >>> 1;
        }
        return count;
    }*/
}
